package com.example.bbgds.test;

import android.animation.Animator;
import android.animation.AnimatorSet;
import android.animation.ObjectAnimator;
import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.util.AttributeSet;
import android.view.View;
import android.widget.RelativeLayout;

/**
 * Created by bbgds on 2016/3/7.
 */
public class VoiceAnimationItem extends RelativeLayout{

    private static final long DURATION = 300;
    private LayoutBottom mLayoutBottom;
    private View mCurrentView;
    private View mNextView;
    private AnimatorSet mCurAnimatorSet, mNextAnimatorSet;

    public VoiceAnimationItem(Context context) {
        this(context, null);
    }

    public VoiceAnimationItem(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public VoiceAnimationItem(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        post(new Runnable() {
            @Override
            public void run() {
                setParentHeight(1000);
            }
        });
    }

    public void setLayoutBottom(LayoutBottom view){
        mLayoutBottom = view;
    }

    public void addItem(View view, DataItem data, int itemHeight){
        if(mCurAnimatorSet != null && mCurAnimatorSet.isRunning()){
            mCurAnimatorSet.cancel();
            mCurAnimatorSet = null;
        }
        int parentHeight = getHeight();
        RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
        lp.leftMargin = 100;
        lp.rightMargin = 100;
        lp.addRule(RelativeLayout.CENTER_HORIZONTAL, RelativeLayout.TRUE);
        view.setTag(data);
        view.setLayoutParams(lp);
        showNextAnimation(view);
        addView(view);
        mNextView = view;
        if (mCurrentView != null) {
            addCurrentItemToList((DataItem) mCurrentView.getTag(), itemHeight, parentHeight);
        } else {
            addFirstItemAnimation(getHeight(), itemHeight);
            mCurrentView = view;
        }
    }

    private void addCurrentItemToList(final DataItem item, final int itemHeight, int parentHeight){
        ObjectAnimator translationX;
        if(item.isRight){
            translationX = ObjectAnimator.ofFloat(mCurrentView, "translationX", 0, getWidth() - mCurrentView.getWidth() - mCurrentView.getX());
        }else{
            translationX = ObjectAnimator.ofFloat(mCurrentView, "translationX", 0, -mCurrentView.getX());
        }
        ObjectAnimator alpha = ObjectAnimator.ofFloat(mCurrentView, "alpha", 1f, 0.5f);
        int addHeight = itemHeight;
        ObjectAnimator translation = ObjectAnimator.ofFloat(this, "parentHeight", parentHeight, parentHeight + addHeight);
        int startMarginTop = mCurrentView.getHeight() - itemHeight;
        ObjectAnimator topmarGIn = ObjectAnimator.ofFloat(this, "nextViewMarginTop", startMarginTop, startMarginTop + itemHeight);
        mCurAnimatorSet = new AnimatorSet();
        mCurAnimatorSet.setDuration(DURATION);
        mCurAnimatorSet.playTogether(translationX, alpha, translation, topmarGIn);
        mCurAnimatorSet.addListener(new Animator.AnimatorListener() {
            @Override
            public void onAnimationStart(Animator animator) {

            }

            @Override
            public void onAnimationEnd(Animator animator) {
                addCurrentItemAnimationEnd(item, itemHeight);
            }

            @Override
            public void onAnimationCancel(Animator animator) {
                addCurrentItemAnimationEnd(item, itemHeight);
            }

            @Override
            public void onAnimationRepeat(Animator animator) {

            }
        });
        mCurAnimatorSet.start();
    }

    private void addCurrentItemAnimationEnd(DataItem item,  int itemHeight){
        removeView(mCurrentView);
        mCurrentView = mNextView;
        setParentHeight(itemHeight);
        setNextViewMarginTop(0);
        mLayoutBottom.addItemToList(item);
    }


    private void setNextViewMarginTop(float margin){
        RelativeLayout.LayoutParams lp = (RelativeLayout.LayoutParams)mNextView.getLayoutParams();
        lp.topMargin = (int)margin;
        mNextView.setLayoutParams(lp);
    }

    private void setParentHeight(float height){
        RecyclerView.LayoutParams lp = (RecyclerView.LayoutParams)getLayoutParams();
        if(lp == null ){
            lp = new RecyclerView.LayoutParams(RecyclerView.LayoutParams.MATCH_PARENT, (int)height);
        }
        lp.height = (int)height;
        lp.width = RecyclerView.LayoutParams.MATCH_PARENT;
        this.setLayoutParams(lp);
    }

    private void showNextAnimation(final View view){
        if(mNextAnimatorSet != null && mNextAnimatorSet.isRunning()){
            mNextAnimatorSet.cancel();
            mNextAnimatorSet = null;
        }
        ObjectAnimator translation = ObjectAnimator.ofFloat(view, "translationY", 200, 0);
        translation.setDuration(DURATION);
        ObjectAnimator scaleX = ObjectAnimator.ofFloat(view, "scaleX", 0, 1f);
        scaleX.setDuration(DURATION);
        ObjectAnimator scaleY = ObjectAnimator.ofFloat(view, "scaleY", 0, 1f);
        scaleY.setDuration(DURATION);
        mNextAnimatorSet = new AnimatorSet();
        mNextAnimatorSet.playTogether(scaleX, scaleY, translation);
        mNextAnimatorSet.addListener(new Animator.AnimatorListener() {
            @Override
            public void onAnimationStart(Animator animator) {

            }

            @Override
            public void onAnimationEnd(Animator animator) {

            }

            @Override
            public void onAnimationCancel(Animator animator) {
                view.setScaleX(1f);
                view.setScaleY(1f);
                view.setTranslationY(0);
            }

            @Override
            public void onAnimationRepeat(Animator animator) {

            }
        });
        mNextAnimatorSet.start();
    }

    private void addFirstItemAnimation(int parentHeight, int height){
        ObjectAnimator translation = ObjectAnimator.ofFloat(this, "parentHeight", parentHeight, height);
        translation.setDuration(DURATION);
        translation.start();
    }
}
